package com.hingsmy.pma.controllers;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hingsmy.pma.dao.ProjectRepository;
import com.hingsmy.pma.dao.StudentRepository;

import com.hingsmy.pma.dto.ChartData;
import com.hingsmy.pma.dto.StudentProject;
import com.hingsmy.pma.entities.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.SpringVersion;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class HomeController {
    @Value("${version}")
    private String version;
    @Autowired
    ProjectRepository projectRepository;
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/")
    public String displayHome(Model model) throws JsonProcessingException {
        model.addAttribute("versionNumber",version);
        Map<String,Object> map=new HashMap<>();
        List<Project> projects = projectRepository.findAll();
        model.addAttribute("projects",projects);

        List<ChartData> projectStatusData=projectRepository.getProjectStatus();
        ObjectMapper objectMapper=new ObjectMapper();
        String jsonString=objectMapper.writeValueAsString(projectStatusData);
        model.addAttribute("projectStatusData",jsonString);
        System.out.println(jsonString);

        List<StudentProject> studentprojects = studentRepository.studentprojects();
        model.addAttribute("studentProjects",studentprojects);

        return "main/home";


    }
}
